package android.explorer.activity;
import myultility.Exec;
import android.app.Activity;
import android.content.DialogInterface;
import android.explorer.R;
import android.explorer.dialogUltility.ShowOkDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class ChangePermission extends Activity{
	String permission,filename;
	int last;
	CheckBox cbOr,cbOw,cbOe,cbGr,cbGw,cbGe,cbAr,cbAw,cbAe;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.changepermission);
		filename=this.getIntent().getExtras().getString("filename");
		permission=this.getIntent().getExtras().getString("permission");
		last=getLastMode();
		System.out.println(permission);
		cbOr=(CheckBox)findViewById(R.id.ownerpermissionread);
		cbOw=(CheckBox)findViewById(R.id.ownerpermissionwrite);
		cbOe=(CheckBox)findViewById(R.id.ownerpermissionexecute);
		
		cbGr=(CheckBox)findViewById(R.id.grouppermissionread);
		cbGw=(CheckBox)findViewById(R.id.grouppermissionwrite);
		cbGe=(CheckBox)findViewById(R.id.grouppermissionexecute);
		
		cbAr=(CheckBox)findViewById(R.id.otherpermissionread);
		cbAw=(CheckBox)findViewById(R.id.otherpermissionwrite);
		cbAe=(CheckBox)findViewById(R.id.otherpermissionexecute);
		reset();
	}
	public void onFinishClick(View target) {
		Exec exe=new Exec(new String[] {"chmod",String.valueOf(getMode()),filename});
		System.out.println("File name="+filename);
		if(exe.exe(20)){
			new ShowOkDialog(this,"Operation finish failed.\n"+exe.error,"Error",new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int arg1) {
					dialog.cancel();
					finish();
				}
			});
		}else{
			Toast.makeText(this, "Operation finish successly.", Toast.LENGTH_LONG).show();
		}
		System.out.println(exe.error+" | "+exe.result);
		
	}
	public void onResetClick(View target) {
		reset();
	}
	public void onCancelClick(View target) {
		finish();
	}
	public void reset(){
		cbOr.setChecked(permission.charAt(0)=='r');
		cbOw.setChecked(permission.charAt(1)=='w');
		cbOe.setChecked(permission.charAt(2)=='x');
		
		cbGr.setChecked(permission.charAt(3)=='r');
		cbGw.setChecked(permission.charAt(4)=='w');
		cbGe.setChecked(permission.charAt(5)=='x');
		
		cbAr.setChecked(permission.charAt(6)=='r');
		cbAw.setChecked(permission.charAt(7)=='w');
		cbAe.setChecked(permission.charAt(8)=='x');
	}
	public int getMode(){
		int a=0,b=0,c=0;
		if(cbOr.isChecked())a=4;
		if(cbOw.isChecked())a+=2;
		if(cbOe.isChecked())a+=1;
		
		if(cbGr.isChecked())b=4;
		if(cbGw.isChecked())b+=2;
		if(cbGe.isChecked())b+=1;
		
		if(cbAr.isChecked())c=4;
		if(cbAw.isChecked())c+=2;
		if(cbAe.isChecked())c+=1;
		
		return 100*a+10*b+c;
	}
	public int getLastMode(){
		int a=0,b=0,c=0;
		if(permission.charAt(0)=='r')a=4;
		if(permission.charAt(1)=='w')a+=2;
		if(permission.charAt(2)=='x')a+=1;

		if(permission.charAt(3)=='r')b=4;
		if(permission.charAt(4)=='w')b+=2;
		if(permission.charAt(5)=='x')b+=1;
		
		if(permission.charAt(6)=='r')c=4;
		if(permission.charAt(7)=='w')c+=2;
		if(permission.charAt(8)=='x')c+=1;
		
		return a*100+10*b+c;
	}
}
